package com.iqiyi.pps.epg.core.message;

import com.iqiyi.kiwi.message.Processor;
import com.iqiyi.pps.epg.core.model.log.InterfaceLog;
import com.iqiyi.pps.epg.core.service.log.InterfaceLogService;
import com.iqiyi.pps.epg.core.utils.ApplicationContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.util.Map;

public class InterfaceStatMessageProcessor extends Processor {
    private final Logger logger = LoggerFactory.getLogger(InterfaceStatMessageProcessor.class);

    @Override
    public boolean process() {
        Map<String, String> message = (Map) this.message;
        try {
            InterfaceLog interfaceLog = new InterfaceLog();
            interfaceLog.setTypeId(Byte.valueOf(message.get("typeId")));
            interfaceLog.setPlatform(Byte.valueOf(message.get("platform")));
            interfaceLog.setAppkey(message.get("appkey"));
            interfaceLog.setName(message.get("name"));
            interfaceLog.setServerIp(message.get("serverIp"));
            interfaceLog.setParams(message.get("params"));
            interfaceLog.setResponse(message.get("response"));
            Long reqTime = Long.valueOf(message.get("reqTime"));
            Long resTime = Long.valueOf(message.get("resTime"));
            interfaceLog.setReqTime(new Timestamp(reqTime));
            interfaceLog.setResTime(new Timestamp(resTime));
            interfaceLog.setUsageTime((int) (resTime - reqTime));
            InterfaceLogService interfaceLogService = (InterfaceLogService) ApplicationContextUtil.getBean(InterfaceLogService.class);
            interfaceLogService.save(interfaceLog);
        } catch (Exception e) {
            logger.error("[InterfaceStatMessageProcessor][process][error][message={}]", this.message, e);
        }
        finished(true);
        return true;
    }
}
